﻿#region Header

/**************************************
 * FILE:          Program.cs
 * DATE:          13.08.2009 16:46:02
 * AUTHOR:        Raphael B. Estrada
 * AUTHOR URL:    http://www.galaktor.net
 * AUTHOR E-MAIL: galaktor@gmx.de
 *
 * The MIT License
 *
 * Copyright (c) 2009 Raphael B. Estrada
 * Author URL:    http://www.galaktor.net
 * Author E-Mail: galaktor@gmx.de
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * ***********************************/

#endregion Header

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

using net.galaktor.RHFSM;

namespace net.galaktor.RHFSMDemo
{
    /// <summary>
    /// This application demonstrates the usage of RHFSM with a very simple example.
    /// Imagine having a game that consists of two main states, "Menu" and "Game".
    /// When you change to the state "Menu", it should enter "SubMenu" automatically, without
    /// you having to tell it explicitly. This could be changed later on without having
    /// to alter any calls to the machine itselft, only within the said states.
    /// This program first enters the state "Game". After 5 seconds, Game requests a
    /// transition to "Menu", which will enter "SubMenu" automatically. SubMenu will
    /// request a transition back to Game after 5 seconds. Every second the "Update"
    /// method is called on the root state causing all states up to the active state
    /// to perform whatever is inside their "Update" methods. To keep it simple, all
    /// they do here is print their absolute path within the machine.
    /// </summary>
    class Program
    {
        #region Methods

        #region Private Static Methods

        static void Main( string[] args )
        {
            using ( IMachine machine = new Machine() )
            {
                // add 1st level state "Menu" using generic factory method
                // machine has no knowledge of the 2nd level state "SubMenu", but "Menu" does
                machine.RootState.AddSubState<Menu>();

                // add 1st level state "Game" using the alternative add method
                IState game = new Game();
                machine.RootState.AddSubState( game );

                // initialize the machine by starting it off at state "Game"
                // this could be any state as long as the correct path is given
                // here it is easy since Game is a first level state so it's path
                // is simply it's name
                machine.Initialize( "Game" );

                // demonstrates how to retrieve a substate
                string requestedSubStatePath = State.CombineStateNames( "Menu", "SubMenu" );

                // use substate path relative to the machine root to retrieve the state ia the GetSubState method
                // this can be done on any state
                IState subMenu = machine.RootState.GetSubState( requestedSubStatePath );

                // use substate path relative to "subMenu" as index key, which effectively just
                // calls GetSubState
                subMenu = machine.RootState[ requestedSubStatePath ];

                // call update every second
                while ( true )
                {
                    machine.ActiveState.Update();
                    Thread.Sleep( 1000 );
                }
            }
        }

        #endregion Private Static Methods

        #endregion Methods
    }
}